using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using WIT.DAQ.Core.Models;

namespace WIT.DAQ.Core.Forms;

/// <summary>
///
/// </summary>
internal class TaskConditionEditForm : BaseForm
{
	/// <summary>
	///
	/// </summary>
	/// <param name="condition"></param>
	internal delegate void SaveTaskConditionHandler(TaskCondition condition);

	/// <summary>
	/// 当前操作的站点
	/// </summary>
	private readonly Station STATION;

	/// <summary>
	/// 实例化的目的
	/// </summary>
	internal readonly EnumFormPurpose PURPOSE;

	/// <summary>
	/// 当次编辑的触发条件
	/// </summary>
	internal readonly TaskCondition CONDITION;

	private const string TITLE_ADD = "添加采集项";

	private const string TITLE_EDIT = "编辑采集项";

	/// <summary>
	/// Required designer variable.
	/// </summary>
	private IContainer components = null;

	private Label label1;

	private TextBox txtValue;

	private ComboBox cmbDAQItems;

	private ComboBox cmbOperators;

	private Label label2;

	private Label label3;

	private Button btnConfirm;

	private Button btnCancel;

	/// <summary>
	///
	/// </summary>
	internal event SaveTaskConditionHandler SaveTaskCondition;

	/// <summary>
	///
	/// </summary>
	/// <param name="station"></param>
	/// <param name="purpose"></param>
	/// <param name="condition"></param>
	internal TaskConditionEditForm(Station station, EnumFormPurpose purpose, TaskCondition condition = null)
	{
		InitializeComponent();
		InitDAQItems(station.Id);
		InitOperators();
		STATION = station;
		PURPOSE = purpose;
		CONDITION = condition;
		switch (PURPOSE)
		{
		case EnumFormPurpose.Add:
			Text = "添加采集项";
			break;
		case EnumFormPurpose.Edit:
			Text = "编辑采集项";
			cmbDAQItems.SelectedValue = CONDITION.DAQItemId;
			cmbOperators.SelectedValue = CONDITION.Operator;
			txtValue.Text = CONDITION.Value.ToString();
			break;
		}
	}

	private void TaskConditionEditForm_Load(object sender, EventArgs e)
	{
		txtValue.Select();
	}

	/// <summary>
	/// 加载采集项
	/// </summary>
	private void InitDAQItems(Guid stationId)
	{
		List<DAQItem> dAQItems = Common.GetDAQItems(stationId);
		cmbDAQItems.DisplayMember = "ShowName";
		cmbDAQItems.ValueMember = "Id";
		cmbDAQItems.DataSource = dAQItems;
	}

	/// <summary>
	/// 加载运算符
	/// </summary>
	private void InitOperators()
	{
		List<object> dataSource = new List<object>
		{
			new
			{
				Name = "=",
				Value = "="
			},
			new
			{
				Name = "!=",
				Value = "!="
			},
			new
			{
				Name = ">=",
				Value = ">="
			},
			new
			{
				Name = ">",
				Value = ">"
			},
			new
			{
				Name = "<=",
				Value = "<="
			},
			new
			{
				Name = "<",
				Value = "<"
			}
		};
		cmbOperators.DisplayMember = "Name";
		cmbOperators.ValueMember = "Name";
		cmbOperators.DataSource = dataSource;
	}

	/// <summary>
	/// 确认
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	private void btnConfirm_Click(object sender, EventArgs e)
	{
		Guid guid = new Guid(cmbDAQItems.SelectedValue.ToString());
		TaskCondition taskCondition = null;
		switch (PURPOSE)
		{
		case EnumFormPurpose.Add:
			taskCondition = new TaskCondition
			{
				Id = Guid.NewGuid(),
				StationId = STATION.Id,
				DAQItemId = guid,
				DAQItem = Common.GetDAQItem(STATION.Id, guid),
				Operator = cmbOperators.SelectedValue.ToString(),
				Value = txtValue.Text.Trim()
			};
			break;
		case EnumFormPurpose.Edit:
			taskCondition = CONDITION;
			taskCondition.DAQItemId = guid;
			taskCondition.DAQItem = Common.GetDAQItem(STATION.Id, guid);
			taskCondition.Operator = cmbOperators.SelectedValue.ToString();
			taskCondition.Value = txtValue.Text.Trim();
			break;
		}
		this.SaveTaskCondition(taskCondition);
		Close();
	}

	/// <summary>
	/// 关闭
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	private void btnCancel_Click(object sender, EventArgs e)
	{
		Close();
	}

	/// <summary>
	/// ESC关闭
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	private void TaskConditionEditForm_KeyDown(object sender, KeyEventArgs e)
	{
		if (e.KeyCode == Keys.Escape)
		{
			btnCancel_Click(null, null);
		}
	}

	/// <summary>
	/// Clean up any resources being used.
	/// </summary>
	/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
	protected override void Dispose(bool disposing)
	{
		if (disposing && components != null)
		{
			components.Dispose();
		}
		base.Dispose(disposing);
	}

	/// <summary>
	/// Required method for Designer support - do not modify
	/// the contents of this method with the code editor.
	/// </summary>
	private void InitializeComponent()
	{
		this.label1 = new System.Windows.Forms.Label();
		this.txtValue = new System.Windows.Forms.TextBox();
		this.cmbDAQItems = new System.Windows.Forms.ComboBox();
		this.cmbOperators = new System.Windows.Forms.ComboBox();
		this.label2 = new System.Windows.Forms.Label();
		this.label3 = new System.Windows.Forms.Label();
		this.btnConfirm = new System.Windows.Forms.Button();
		this.btnCancel = new System.Windows.Forms.Button();
		base.SuspendLayout();
		this.label1.AutoSize = true;
		this.label1.Location = new System.Drawing.Point(27, 27);
		this.label1.Name = "label1";
		this.label1.Size = new System.Drawing.Size(53, 12);
		this.label1.TabIndex = 0;
		this.label1.Text = "采集项：";
		this.txtValue.Location = new System.Drawing.Point(86, 76);
		this.txtValue.Name = "txtValue";
		this.txtValue.Size = new System.Drawing.Size(150, 21);
		this.txtValue.TabIndex = 2;
		this.cmbDAQItems.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
		this.cmbDAQItems.FormattingEnabled = true;
		this.cmbDAQItems.Location = new System.Drawing.Point(86, 24);
		this.cmbDAQItems.Name = "cmbDAQItems";
		this.cmbDAQItems.Size = new System.Drawing.Size(300, 20);
		this.cmbDAQItems.TabIndex = 0;
		this.cmbOperators.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
		this.cmbOperators.FormattingEnabled = true;
		this.cmbOperators.Location = new System.Drawing.Point(86, 50);
		this.cmbOperators.Name = "cmbOperators";
		this.cmbOperators.Size = new System.Drawing.Size(150, 20);
		this.cmbOperators.TabIndex = 1;
		this.label2.AutoSize = true;
		this.label2.Location = new System.Drawing.Point(27, 53);
		this.label2.Name = "label2";
		this.label2.Size = new System.Drawing.Size(53, 12);
		this.label2.TabIndex = 0;
		this.label2.Text = "运算符：";
		this.label3.AutoSize = true;
		this.label3.Location = new System.Drawing.Point(51, 79);
		this.label3.Name = "label3";
		this.label3.Size = new System.Drawing.Size(29, 12);
		this.label3.TabIndex = 0;
		this.label3.Text = "值：";
		this.btnConfirm.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right;
		this.btnConfirm.Location = new System.Drawing.Point(311, 156);
		this.btnConfirm.Name = "btnConfirm";
		this.btnConfirm.Size = new System.Drawing.Size(75, 23);
		this.btnConfirm.TabIndex = 3;
		this.btnConfirm.Text = "确认(&S)";
		this.btnConfirm.UseVisualStyleBackColor = true;
		this.btnConfirm.Click += new System.EventHandler(btnConfirm_Click);
		this.btnCancel.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right;
		this.btnCancel.Location = new System.Drawing.Point(392, 156);
		this.btnCancel.Name = "btnCancel";
		this.btnCancel.Size = new System.Drawing.Size(75, 23);
		this.btnCancel.TabIndex = 4;
		this.btnCancel.Text = "取消(&C)";
		this.btnCancel.UseVisualStyleBackColor = true;
		this.btnCancel.Click += new System.EventHandler(btnCancel_Click);
		base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 12f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		base.ClientSize = new System.Drawing.Size(479, 191);
		base.Controls.Add(this.btnConfirm);
		base.Controls.Add(this.btnCancel);
		base.Controls.Add(this.cmbOperators);
		base.Controls.Add(this.cmbDAQItems);
		base.Controls.Add(this.txtValue);
		base.Controls.Add(this.label3);
		base.Controls.Add(this.label2);
		base.Controls.Add(this.label1);
		base.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
		base.MaximizeBox = false;
		base.Name = "TaskConditionEditForm";
		this.Text = "TaskConditionEditForm";
		base.Load += new System.EventHandler(TaskConditionEditForm_Load);
		base.KeyDown += new System.Windows.Forms.KeyEventHandler(TaskConditionEditForm_KeyDown);
		base.ResumeLayout(false);
		base.PerformLayout();
	}
}
